package com.biometric.core.retriever

import android.os.Build
import androidx.annotation.Keep
import androidx.biometric.BiometricManager
import androidx.core.hardware.fingerprint.FingerprintManagerCompat
import com.biometric.core.support.FaceAuthCallableRule
import com.biometric.core.support.FaceSupportManager
@Keep
object DefaultBiometricMetaDataRetriever : BiometricMetaDataRetriever {
    private val biometricManager by lazy { BiometricManager.from(AppContext.get()) }
    private val fingerprintManagerCompat by lazy { FingerprintManagerCompat.from(AppContext.get()) }
    private var faceAuthCallableRule = FaceAuthCallableRule.default

    /**
     * 当前人脸识别是否可以调用
     */
    override fun isFaceAuthenticationEnabled(): Boolean {
        if (FaceSupportManager.isDeviceSupportFaceAuthentication()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

                return faceAuthCallableRule.isCallable()
            }
        }

        return false
    }

    /**
     * 替换判断设备人脸验证可以调用的规则
     */
    fun updateFaceRule(newRule: FaceAuthCallableRule) {
        this.faceAuthCallableRule = newRule
    }

    override fun canBiometricAuthenticate(): Boolean {
        return biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) == BiometricManager.BIOMETRIC_SUCCESS
    }

    override fun isFingerprintAuthenticationEnabled(): Boolean {
        return isFingerprintHardwareEnabled() && fingerprintManagerCompat.hasEnrolledFingerprints()
    }

    override fun isFingerprintHardwareEnabled(): Boolean {
        return fingerprintManagerCompat.isHardwareDetected
    }

}